﻿#include "zmqpublisher.h"
#include "widgettcpcfg.h"

ZmqPublisher::ZmqPublisher(QObject *parent) : BasePublisher(parent)
{

}

bool ZmqPublisher::connectToHost()
{
    _context=zmq_ctx_new();
    _socket=zmq_socket(_context,ZMQ_PUB);
    QString commParam=getCommParam();
    int rc=zmq_bind(_socket,commParam.toUtf8());
    int64_t max_msg_size = 8 * 1024 * 1024;
    zmq_setsockopt(_socket,ZMQ_MAXMSGSIZE,&max_msg_size,sizeof(max_msg_size));
    qDebug()<<__FUNCTION__<<__LINE__<<commParam<<rc<<zmq_strerror(rc);
    if(rc!=0)
    {
        return false;
    }

    return true;
}

bool ZmqPublisher::isConnected()
{
    return true;
}

bool ZmqPublisher::disConnect()
{
    zmq_close (_socket);
    zmq_ctx_destroy (_context);
    return true;
}

bool ZmqPublisher::publish(QByteArray data)
{
    int dataLength=data.length();
    bool isOk=zmq_send(_socket,data.data(),dataLength,0)==dataLength;
    return isOk;
}

QWidget *ZmqPublisher::getConfigWidget()
{
    WidgetTcpCfg *cfg=new WidgetTcpCfg();
    cfg->setComm(this);
    return cfg;
}
